package org.apache.struts.ehr.patient_details.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.ehr.patient_details.model.EthnicGroup;
import org.apache.struts.ehr.patient_details.model.HL7Message;
import org.apache.struts.ehr.patient_details.model.PatientDetails;
import org.apache.struts.ehr.patient_details.model.ReligiousAffliation;
import org.apache.struts2.dispatcher.StrutsRequestWrapper;
import org.apache.struts2.interceptor.ServletRequestAware;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v251.message.ADR_A19;
import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
import ca.uhn.hl7v2.parser.PipeParser;

import com.earogya.communicate.Communicator;
import com.earogya.logic.patient_details.ADR_A19_Handler;
import com.earogya.logic.patient_details.CreateQRYA19;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class MoreSearchAction extends ActionSupport implements ServletRequestAware{

	private PatientDetails patientDetails;
	private HL7Message message;
	private Date date;
	private MoreSearchResultsAction action;
	private HttpServletRequest request;
	private List <PatientDetails> patientDetailsList;

	private List<String> genderList;
	private List<String> prefixList;
	private List<String> maritialStatusList;
	private List<ReligiousAffliation> religionList;
	private List<EthnicGroup> ethnicGroupList;
	private List<String>countryList;
	
	public HL7Message getMessage() {
		return message;
	}

	public MoreSearchAction(){
		patientDetails=new PatientDetails();
	}
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}
	public PatientDetails getPatientDetails() {
		return patientDetails;
	}

	public void setPatientDetails(PatientDetails patientDetails) {
		this.patientDetails = patientDetails;
	}

	public String execute() throws Exception {
		Map<String, Object> session=ActionContext.getContext().getSession();
		patientDetails.getPatient().setPatientId(Integer.parseInt(request.getParameter("patient_id")));
	
		//StrutsRequestWrapper.this.getAttribute("patient_id");
		//patientDetails.getPatient().setPatientId(request.getParameter("patient_id"));
		CreateQRYA19 createMessage=new CreateQRYA19(patientDetails);
		String msg=createMessage.createMessage((String)session.get("facility"));
		message=new HL7Message();
		if(!msg.equals(null)){
			Communicator c=new Communicator();
			String reply=c.sendMessage(msg);
			message.setMessage(msg+"-------------"+reply);
			System.out.print(reply);
			//action=new MoreSearchResultsAction(reply);
			//action.execute();
			ADR_A19_Handler handler=new ADR_A19_Handler(createADRA19Msg(reply.toString()));
			patientDetailsList=handler.manageMessage();
		this.patientDetails=patientDetailsList.get(0);
			
		}
				else{
			message.setMessage("empty");
		}
		genderList = new ArrayList<String>();
		genderList.add("Male");
		genderList.add("Female");
		
		maritialStatusList=new ArrayList<String>();
		maritialStatusList.add("Single");
		maritialStatusList.add("Married");
		religionList=new ArrayList<ReligiousAffliation>();
		
		religionList.add(new ReligiousAffliation(1,"BU","Buddhism"));
		religionList.add(new ReligiousAffliation(2,"MU","Muslim"));
		ethnicGroupList=new ArrayList<EthnicGroup>();
		ethnicGroupList.add(new EthnicGroup(1,"SI","Sinhalese"));
		ethnicGroupList.add(new EthnicGroup(2,"TA","Tamil"));
		
		countryList=new ArrayList<String>();
		countryList.add("Sri Lanka");
		countryList.add("USA");
		
		prefixList=new ArrayList<String>();
		prefixList.add("Rev");
		prefixList.add("Mr");
		prefixList.add("Miss");
		prefixList.add("Mrs");
		return SUCCESS;

	}
	
	
	
	public MoreSearchResultsAction getAction() {
		return action;
	}

	public void setAction(MoreSearchResultsAction action) {
		this.action = action;
	}
	
	public List<String> getGenderList() {
		return genderList;
	}


	public void setGenderList(List<String> genderList) {
		this.genderList = genderList;
	}


	public List<String> getMaritialStatusList() {
		return maritialStatusList;
	}


	public void setMaritialStatusList(List<String> maritialStatusList) {
		this.maritialStatusList = maritialStatusList;
	}


	public List<ReligiousAffliation> getReligionList() {
		return religionList;
	}


	public void setReligionList(List<ReligiousAffliation> religionList) {
		this.religionList = religionList;
	}


	public List<EthnicGroup> getEthnicGroupList() {
		return ethnicGroupList;
	}


	public void setEthnicGroupList(List<EthnicGroup> ethnicGroupList) {
		this.ethnicGroupList = ethnicGroupList;
	}


	public List<String> getCountryList() {
		return countryList;
	}


	public void setCountryList(List<String> countryList) {
		this.countryList = countryList;
	}


	
	public List<String> getPrefixList() {
		return prefixList;
	}

	public void setPrefixList(List<String> prefixList) {
		this.prefixList = prefixList;
	}

	public ADR_A19 createADRA19Msg(String message) throws EncodingNotSupportedException, HL7Exception{
		Message hapiMsg  = new PipeParser().parse(message);
		return (ADR_A19)hapiMsg;
	}
	
	public void validate(){
		
	}
	
	private Boolean isInteger(String value){
		Boolean isInteger=false;
		try{
			int intValue=Integer.parseInt(value);
			isInteger=true;
		}catch(NumberFormatException e){
			isInteger=false;
		}
		return isInteger;
	}
	
	private Boolean isCorrectNICNo(String nic){
		Boolean isCorrectNicNo=false;
		int length=nic.length();
		if(length==10 && (nic.charAt(9)=='v' || nic.charAt(9)=='V')){
			isCorrectNicNo=true;
		}
		return isCorrectNicNo;
	}
	
	private Boolean isCorrectTelephoneNumber(String number){
		Boolean isCorrectNumber=false;
		String[] numbers=number.split(",");
		for(int i=0; i<numbers.length; i++){
			String num=numbers[i].trim();
			if(num.matches("[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]")){
				isCorrectNumber=true;
			}else if(num.matches("[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]")){
				isCorrectNumber=true;
			}
		}
		return isCorrectNumber;
	}
	
	private Boolean isCorrectEmailAddress(String email){
		Boolean isCorrectEmail=false;
		if(email.matches("[0-9a-z]+@[a-z]+.[a-z]+")){
			isCorrectEmail=true;
		}
		return isCorrectEmail;
	}
}
